﻿#include "mimashezhi.h"
#include "ui_mimashezhi.h"
#include<QFile>
#include<QString>
#include<QIODevice>
#include<QDebug>
#include <QTreeWidgetItem>
#include<QMessageBox>
#include<QDateTime>

mimashezhi::mimashezhi(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mimashezhi)
{
    ui->setupUi(this);
}

mimashezhi::~mimashezhi()
{
    delete ui;
}

void mimashezhi::on_pushButton_clicked()
{

    QString pass;
    //获取密码数据库
    QFile file("D:/password.txt");
    if (!file.open(QIODevice::ReadOnly	 | QIODevice::Text))
    {
        qDebug()<<"log读取失败";
        return;
    }
    //构建QTextStream读取文本
    QTextStream in(&file);
    //将应用程序的光标设置为等待状态
    QApplication::setOverrideCursor(Qt::WaitCursor);
    //转码
    in.setCodec("UTF-8");
    //显示到textEdit文本框
    pass = in.readAll();
    //读取完成后恢复光标状态
    QApplication::restoreOverrideCursor();
    qDebug()<<"log读取"<<pass;

    QString us = ui->lineEdit->text().toUtf8().data();//原密码
    QString pa = ui->lineEdit_2->text().toUtf8().data();//新密吗
    QString pa1 = ui->lineEdit_3->text().toUtf8().data();//新密吗
    //用户名密码核对
    if(pass.contains(us,Qt::CaseSensitive)  && pa!=NULL && pa1 != NULL && pa.compare(pa1) == 0)
    {
        QFile file1("D:/password.txt");
        if (!file1.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            qDebug()<<"数据库读取失败";
            return;
        }
        while (!file1.atEnd()) {
            pass = file1.readLine();
        }

        pass.replace(us,pa);
        QByteArray  strBytes=pass.toUtf8();//转换为字节数组
        file1.write(strBytes,strBytes.length());  //写入文件
        file1.close();
        //qDebug()<<"密码修改后"<<pas;

        //QMessageBox::critical(this,"提示","修改成功");
        QMessageBox::information(this,"提示","修改成功");



        QDateTime time = QDateTime::currentDateTime();
        QString mytime = time.toString("yyyy-MM-dd hh:mm:ss");//"yyyy-MM-dd hh:mm:ss dddd"
        QString pasa;
        QFile file11("D:/mylog.txt");
        if (!file11.open(QIODevice::Append | QIODevice::Text))
        {
            //qDebug()<<"数据库读取失败";
            return;
        }
        while (!file11.atEnd()) {
            pasa = file11.readLine();
        }
        //qDebug()<<"密码修改前"<<pas;
        pasa="修改了密码";
        QByteArray  strBytes1=pasa.toUtf8();//转换为字节数组
        file11.write(strBytes1,strBytes1.length());  //写入文件
        pasa="\n"+mytime+"\n";
        QByteArray  strBytes11=pasa.toUtf8();//转换为字节数组
        file11.write(strBytes11,strBytes11.length());  //写入文件
        //qDebug()<<"密码修改后"<<pas;
        file11.close();

    }else
    {
        QMessageBox::critical(this,"警告","原密码错误或两次密码不一致");
        QDateTime time = QDateTime::currentDateTime();

        QString mytime = time.toString("yyyy-MM-dd hh:mm:ss");//"yyyy-MM-dd hh:mm:ss dddd"


        QString pas;
        QFile file1("D:/mylog.txt");
        if (!file1.open(QIODevice::Append | QIODevice::Text))
        {
            //qDebug()<<"数据库读取失败";
            return;
        }
        while (!file1.atEnd()) {
            pas = file1.readLine();
        }
        //qDebug()<<"密码修改前"<<pas;
        pas="密码修改失败";
        QByteArray  strBytes=pas.toUtf8();//转换为字节数组
        file1.write(strBytes,strBytes.length());  //写入文件
        pas="\n"+mytime+"\n";
        QByteArray  strBytes1=pas.toUtf8();//转换为字节数组
        file1.write(strBytes1,strBytes1.length());  //写入文件
        //qDebug()<<"密码修改后"<<pas;
        file1.close();
    }
}
